package tgc.edu.exam.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;

import tgc.edu.exam.custom.BaseEntity;



/**
 * 用户类
 * @author Administrator
 *
 */
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class SysUser extends BaseEntity<Integer>{

	private String username;//用户名
	private String password;//密码
	private String name;//姓名
	
	@JsonIgnore
	private List<SysRole> role = new ArrayList<>();
	

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.EAGER)
	@JoinTable(name="sys_user_role",joinColumns=@JoinColumn(name="user_id"),inverseJoinColumns=@JoinColumn(name="role_id"))
	public List<SysRole> getRole() {
		return role;
	}
	public void setRole(List<SysRole> role) {
		this.role = role; 
	}
	//获取权限代码
		@Transient
		public String getRoleCodes() {
			String str="";
			for (SysRole role : getRole()) {
				str+=role.getCode()+",";
			}
			if(str.indexOf(",")>0) {
				str=str.substring(0,str.length()-1);
			}
			return str;
		}
		
		//获取权限名称	
		@Transient
		public String getRoleNames() {
			String str="";
			for (SysRole role : getRole()) {
				str+=role.getName()+",";
			}
			if(str.length()>0) {
				str=str.substring(0, str.length()-1);
			}
			return str;
		}

	
}
